package models;

import product.Product;
import util.common.ConvertUtil;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;

/**
 * Created by liming on 16/7/12.
 */
@Entity
@Table(name = "admin_supplier_product")
public class AdminSupplierProduct extends BaseModel {

    /**
     * 供应商
     */
    @ManyToOne
    @JoinColumn(name = "admin_supplier_id")
    public AdminSupplier adminSupplier;

    /**
     * 经营产品
     */
    @ManyToOne
    @JoinColumn(name = "product_id")
    public Product product;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    /**
     * 查询供应商产品
     *
     * @param supplierId
     * @return
     */
    public static List<AdminSupplierProduct> findBySupplierId(long supplierId) {
        return AdminSupplierProduct.find("adminSupplier.id = ?", supplierId).fetch();
    }

    /**
     * 删除供应商下的所有产品
     */
    public static void deleteBySupplierId(long id) {
        AdminSupplierProduct.delete("adminSupplier.id = ?", id);
    }

    /**
     * 删除产品
     */
    public static void deleteByIds(Integer[] delIds) {
        String delIdStr = UStringUtil.concatStr(",", delIds);
        AdminSupplierProduct.delete("id in (" + ConvertUtil.escapeSql(delIdStr) + ")");
    }
}
